package com.bulletelc.apollopre.module.main.center.quotation;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.entity.QuotationBean;
import com.bulletelc.apollopre.framework.MVPBaseActivity;
import com.bulletelc.apollopre.module.main.center.quotation.detail.QuotationDetailActivity;
import com.bulletelc.apollopre.utils.ToastUtil;
import com.bulletelc.apollopre.widget.FoolBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/16.
 */

public class QuotationListActivity extends MVPBaseActivity<QuotationListContract.View, QuotationListPresenter> implements QuotationListContract.View,
        FoolBar.ClickCallback, AdapterView.OnItemClickListener, OnRefreshListener {

    // views
    private FoolBar flBar;
    private ListView lvQuotation;
    private SmartRefreshLayout smartRefreshLayout;

    // data
    private int intention_form_id;
    private List<QuotationBean> quotationBeanList;
    private QuotationListAdapter adapter;

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public int getLayoutRes() {
        return R.layout.quotation_list_activity;
    }

    @Override
    public void assignViews() {
        flBar = findView(R.id.flBar);
        lvQuotation = findView(R.id.lvQuotation);
        smartRefreshLayout = findView(R.id.smartRefreshLayout);
    }

    @Override
    public void setListener() {
        flBar.setClickCallback(this);
        lvQuotation.setOnItemClickListener(this);
        smartRefreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (intent != null) {
            intention_form_id = intent.getIntExtra(IntentCode.EXTRA_INTENTION_ORDER_ID, 0);
        }
        flBar.setTitle("定制报价清单");
        quotationBeanList = new ArrayList<>();
        adapter = new QuotationListAdapter(this, quotationBeanList);
        lvQuotation.setAdapter(adapter);
        presenter.getQuotationList(intention_form_id);
    }

    @Override
    public void onSuccessQuotationList(List<QuotationBean> quotationBeanList) {
        if (quotationBeanList.size() > 0) {
            this.quotationBeanList.clear();
            this.quotationBeanList.addAll(quotationBeanList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onErrorQuotationList(String error) {
        ToastUtil.showLongToast(this, R.string.network_msg);
    }

    @Override
    public void onFailQuotationList(String fail) {
        ToastUtil.showLongToast(this, fail);
    }


    @Override
    protected QuotationListPresenter createPresenter() {
        return new QuotationListPresenter();
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(this, QuotationDetailActivity.class);
        intent.putExtra(IntentCode.EXTRA_QUOTATION_ORDER_ID, quotationBeanList.get(i).getId());
        startActivity(intent);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        smartRefreshLayout.finishRefresh(400);
        quotationBeanList.clear();
        presenter.getQuotationList(intention_form_id);
    }
}
